<%--
  ~ Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  ~ Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
  ~ Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
  ~ Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
  ~ Vestibulum commodo. Ut rhoncus gravida arcu.
  --%>

<%--
  Created by IntelliJ IDEA.
  User: dengdc
  Date: 2017/7/19
  Time: 17:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>报价协议管理</title>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript">
        var pickUpDateLayerId = 0;
        var editViewLayerId = 1;
        var formSetting = {
            title: "查询信息",
            module: 'query',
            columns: [{
                name: "supplierId",
                label: "业务供应商",
                type: "dropdown",
                dropdownType: "ajax",
                url: "<s:url value='/order-dropdown/supCompanyByKey'/>",
                op: "eq",
                minLength: 0,
                keyWordName: "key",
                success: function (data) {
                    data = data.results;
                    var array = new Array();
                    $.each(data, function (i, n) {
                        array.push([n["name"], n["value"]]);
                    });
                    return array;
                }
            }, {
                name: "name",
                label: "名称"
            }, {
                name: "status",
                label: "状态",
                type: 'dropdown',
                data: [["草稿", 0], ["生效", 1], ["待确认", 2], ["待生效", 3], ["结束", 4]]
            }, {
                name: "eftimeStart",
                label: "生效时间起",
                type: 'date'
            }, {
                name: "eftimeEnd",
                label: "至",
                type: 'date'
            }],
            buttons: {
                custom: [{
                    id: "addProtocolUnite",
                    value: "添加",
                    index: 1,
                    click: function () {
                        editViewLayerId = layer.open({
                                                         type: 2,
                                                         title: false,
                                                         content: 'addView',
                                                         area: ['840px', '400px'],
                                                         success: function (layero, index) {
                                                             var $layer0 = $(layero[0]);
                                                             var layerIframeBodyHeight = $(
                                                                 $layer0.find(
                                                                     'iframe')[0].contentDocument.body)
                                                                 .height();
                                                             $layer0.height(
                                                                 layerIframeBodyHeight + 60, 2000);
                                                         }
                                                     });
                    }
                }, {
                    id: "chargingTest",
                    value: "测试用",
                    index: 3,
                    click: function () {
                        layer.open({
                                       type: 2,
                                       title: "询价、成本测试",
                                       maxmin: true,
                                       shade: false,
                                       content: '<s:url value="/fund/protocol/test"/>',
                                       area: ['1080px', '640px']
                                   });
                    }
                }]
            },
            grid: {
                id: "protocolGrid",
                title: "查询结果",
                url: "viewData",
                colModel: [{
                    index: "id",
                    name: "id",
                    hidden: true
                }, {
                    name: "supplierName",
                    label: "供应商",
                    align: 'center'
                }, {
                    name: "name",
                    label: "名称",
                    align: 'center'
                }, {
                    name: "eftimeStart",
                    label: "生效时间起",
                    align: 'center'
                }, {
                    name: "eftimeEnd",
                    label: "生效时间止",
                    align: 'center'
                }, {
                    name: "remark",
                    label: "备注",
                    align: 'center'
                }, {
                    name: "file.name",
                    label: "附件",
                    align: 'center',
                    formatter: function (val, opts, row) {
                        if (row.file) {
                            return "<a href='javascript:downloadAtta(\"" + row.file.id + "\")'>"
                                   + val + "</a>"
                        } else {
                            return "";
                        }
                    }
                }, {
                    name: "status",
                    label: "状态",
                    align: 'center',
                    formatter: function (val) {
                        var statusLab = ["草稿", "生效", "待确认", "待生效", "结束"];
                        var colors = ['black', 'blue', '#cd0a0a', 'green', 'black'];
                        return "<label style='color:" + colors[val] + "'>"
                               + statusLab[val]
                               + "</label>";
                    }
                }, {
                    label: "操作",
                    sortable: false,
                    width: 120,
                    align: 'center',
                    buttons: [{
                        key: "id1",
                        value: "编辑",
                        hide: function (rowdata) {
                            return rowdata.status !== "0";
                        },
                        click: function (gridid, rowId, rowdata) {
                            if (rowdata.status.indexOf("草稿") < 0) {
                                layer.msg("非草稿无法修改。");
                                return;
                            }
                            location.href = 'editView?id=' + rowdata.id;
                        }
                    }, {
                        key: "id2",
                        value: "查看",
                        hide: function (rowdata) {
                            return rowdata.status === "0";
                        },
                        click: function (gridid, rowId, rowdata) {
                            location.href = 'readView?id=' + rowdata.id;
                        }
                    }, {
                        key: "id3",
                        value: "发送协议",
                        hide: function (rowdata) {
                            return rowdata.status !== "0";
                        },
                        click: function (gridid, rowId, rowdata) {
                            layer.confirm("确定发送 <label class='bold_red_font'>" + rowdata.name
                                          + " </label> 至 <label class='bold_red_font'>"
                                          + rowdata.supplierName + "</label>", function () {
                                $.ajax({
                                           url: '<s:url value="/fund/protocol/biz/sendUniteToSup"/>',
                                           method: 'post',
                                           data: {uniteId: rowdata.id},
                                           dataType: 'json',
                                           success: function (resp) {
                                               layer.msg(resp.msg);
                                               reloadGridData();
                                           }
                                       });
                            }, function () {
                                layer.msg("取消发送");
                            });
                        }
                    }, {
                        key: "id4",
                        value: "撤回协议",
                        hide: function (rowdata) {
                            return rowdata.status !== "2";
                        },
                        click: function (gridid, rowId, rowdata) {
                            layer.confirm("确定从 <label class='bold_red_font'>" + rowdata.supplierName
                                          + "</label> 撤回 <label class='bold_red_font'>"
                                          + rowdata.name + " </label>", function () {
                                $.ajax({
                                           url: '<s:url value="/fund/protocol/biz/cancelSendUniteFromSup"/>',
                                           method: 'post',
                                           data: {uniteId: rowdata.id},
                                           dataType: 'json',
                                           success: function (resp) {
                                               layer.msg(resp.msg);
                                               reloadGridData();
                                           }
                                       });
                            }, function () {
                                layer.msg("取消撤回");
                            });
                        }
                    }],
                    formatter: $.jqGrid.custom.formatter.button
                }, {
                    name: "submitor",
                    label: "提交时间/提交人",
                    width: 100,
                    align: 'center',
                    formatter: function (val, opt, row) {
                        if (val) {
                            return val + "<br/>" + row.submitTime;
                        } else {
                            return "";
                        }
                    }
                }, {
                    name: "confirmer",
                    label: "确认人/确认时间",
                    width: 100,
                    align: 'center',
                    formatter: function (val, opt, row) {
                        if (val) {
                            return val + "<br/>" + row.confirmTime;
                        } else {
                            return "";
                        }
                    }
                }],
                gridComplete: function () {
                }
            }
        };
        $(document).ready(function () {
            $("#table").mrdForm(formSetting);
            $("#startDateInput").datepicker({
                                                dateFormat: "yy-mm-dd",
                                                showAnim: "slideDown"
                                            });
            $("#endDateInput").datepicker({
                                              dateFormat: "yy-mm-dd",
                                              showAnim: "slideDown"
                                          });
            $("#didPickedDate").click(function () {
                $("#didPickedDate").attr("disabled", "disabled");
                var protocolIds = [];
                var selRows = $("#protocolGrid").gridMethods().getSelectRows();
                for (var i = 0; i < selRows.length; i++) {
                    var selRow = selRows[i];
                    protocolIds.push(selRow.id);
                }
                var startDate = $("#startDateInput").val();
                var endDate = $("#endDateInput").val();
                $.ajax({
                           url: '<s:url value="/fund/protocol/biz/sendPtsToSup"/>',
                           method: 'post',
                           dataType: 'json',
                           data: {
                               protocolIds: protocolIds.join(","),
                               start: startDate,
                               end: endDate
                           },
                           success: function (data) {
                               layer.close(pickUpDateLayerId);
                               layer.msg(data.msg);
                           },
                           complete: function () {
                               $("#didPickedDate").removeAttr("disabled");
                           }
                       })
            });
        });

        function reloadGridData() {
            $("#protocolGrid").gridMethods().reload();
        }

        function closeEditView() {
            layer.close(editViewLayerId);
        }

        function downloadAtta(aid) {
            var url = "<s:url value="/business/downloadSingleBizAttachment"/>?atype=declaOffer&aid="
                      + aid;
            $.download(url);
        }
    </script>
    <style type="text/css">
        .bold_red_font {
            font-family: bold;
            color: red;
        }

        .ui-autocomplete {
            max-height: 300px;
            overflow-y: auto;
        }
    </style>
</head>
<body>
<div id="table"></div>
<div id="pickDate" class="replace-ui-jqgrid ui-widget ui-widget-content ui-corner-all"
     style="display: none">
    <fieldset id="mf_container0"
              class="replace-ui-jqgrid ui-widget ui-widget-content ui-corner-all mf_con">
        <div class="replace-ui-jqgrid-titlebar ui-widget-header ui-corner-top ui-helper-clearfix table_header">
            <a href="javascript:void(0)"
               class="replace-ui-jqgrid-titlebar-close HeaderButton tab_button">
                <span class="ui-icon ui-icon-circle-triangle-s"></span> </a>
            <span class="replace-ui-jqgrid-title">选择有效期</span></div>
        <table id="mf_table0" class="mrdTable" style="width: 100%;">
            <tbody>
            <tr>
                <td id="td_label_12" align="right" valign="middle"
                    style="width: 100px; height: 30px;">
                    <label>有效时间起：</label></td>
                <td id="td_control_12" class="" align="left" valign="middle"
                    style="width: 250px; height: 30px;"><input id="startDateInput" name="start"
                                                               value="" type="text"
                                                               style="width: 98%;"></td>
                <td id="td_label_13" align="right" valign="middle"
                    style="width: 100px; height: 30px;">
                    <label>有效时间止：</label></td>
                <td id="td_control_13" class="" align="left" valign="middle"
                    style="width: 250px; height: 30px;"><input id="endDateInput" name="end"
                                                               value="" type="text"
                                                               style="width: 98%;"></td>
            </tr>
            <tr class="mrdBtnTr">
                <td id="mf_buttons_container" colspan="6" align="right">
                    <input type="button"
                           id="didPickedDate"
                           class="ui-button ui-widget ui-state-default ui-corner-all"
                           role="button"
                           aria-disabled="false"
                           value="发往确认"
                           style="margin: 3px; font-size: 12px;">
                </td>
            </tr>
            </tbody>
        </table>
    </fieldset>
</div>
</body>
</html>
